﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System.Collections.Generic;
using Daidalos.Exception;

namespace Daidalos.VFS
{
    public class TextFile : File
    {
        /// <summary>
        /// 
        /// </summary>
        private System.IO.StreamReader read;

        /// <summary>
        /// 
        /// </summary>
        private System.IO.StreamWriter write;

        /// <summary>
        /// 
        /// </summary>
        private OpenMode openMode;

        /// <summary>
        /// 
        /// </summary>
        public OpenMode OpenMode
        {
            get
            {
                return openMode;
            }
            set
            {
                openMode = value;
            }
        }

        public TextFile(string name, OpenMode openmode)
        {
            if (openmode == OpenMode.READ)
            {
                read = new System.IO.StreamReader(name);
            }
            else if (openmode == OpenMode.WRITE)
            {
                write = new System.IO.StreamWriter(name);
            }
            else if (openmode == OpenMode.HYBRID)
            {
                read = new System.IO.StreamReader(name);
                write = new System.IO.StreamWriter(name);
            }
        }

        public void Close()
        {
        }

        public string ReadLine()
        {
            if (OpenMode == OpenMode.WRITE || OpenMode == OpenMode.APPEND)
            {
                throw new GenericException("File not in read neither in hybrid mode!");
            }
            return read.ReadLine();
        }

        public void Write(string text)
        {
            // Verify arguments.
            if (text == null)
            {
                throw new InvalidArgumentException("Failed to write text due to a null-string!");
            }

            // Is the file read-only?
            if (OpenMode == OpenMode.READ)
            {
                throw new GenericException("Failed to write to read-only file!");
            }

            // Write the text.
            write.WriteLine(text);
        }

        public void WriteLine(string text)
        {
            // Verify arguments.
            if(text==null)
            {
                throw new InvalidArgumentException("Failed to write text due to a null-string!");
            }

            // Write the text.
            Write(text + "\n");
        }

        public void Flush()
        {
            // Is the file read-only?
            if (OpenMode == OpenMode.READ)
            {
                throw new GenericException("Failed to perform Flush() on read-only file!");
            }

            // Flush the file.
            write.Flush();
        }
    }
}
